<script lang="ts" setup>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'
import type { IFilterAttr, IFilterParams, ISubFilter, ISubFilterSale } from '@/types/api/category'

const route = useRoute()
// 数据
const filterData = ref<ISubFilter>()
// 数据加载状态
const filterLoading = ref(false)
// 分类发生变化的时候，需要重新获取筛选数据，因此需要使用到侦听器
watch(
  () => route.params.id,
  (newValue, oldValue) => {
    if (newValue && route.path === '/category/sub/' + newValue) {
      filterLoading.value = true
      newValue &&
        findSubCategoryFilter(route.params.id).then((res) => {
          const { result } = res.data
          // 品牌全部
          result.selectedBrand = ''
          result.brands.unshift({ id: '', name: '全部' })
          // 销售属性全部
          result.saleProperties.forEach((p) => {
            p.selectedProp = ''
            p.properties.unshift({ id: '', name: '全部' })
          })

          filterData.value = res.data.result
          filterLoading.value = false
        })
    }
  },
  { immediate: true }
)

const emit = defineEmits(['filter-change'])

const getFilterParams = () => {
  const filterParams = {} as IFilterParams
  const attrs = [] as IFilterAttr[]
  filterParams.brandId = filterData.value!.selectedBrand
  filterData.value?.saleProperties.forEach((p) => {
    const attr = p.properties.find((attr) => attr.id === p.selectedProp)

    if (attr && attr.id !== undefined) {
      attrs.push({ groupName: p.name, propertyName: attr.name })
    }
  })

  if (attrs.length) filterParams.attrs = attrs

  console.log(filterParams)
  return filterParams
}

const changeBrand = (id: string) => {
  if (filterData.value?.selectedBrand === id) return
  filterData.value!.selectedBrand = id
  emit('filter-change', getFilterParams())
}

const changeProp = (item: ISubFilterSale, propId: string) => {
  if (item.selectedProp === propId) return
  item.selectedProp = propId
  emit('filter-change', getFilterParams())
}
</script>

<template>
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="JavaScript:;"
          v-for="brand in filterData.brands"
          :class="{ active: filterData.selectedBrand === brand.id }"
          :key="brand.id"
          @click="changeBrand(brand.id)"
        >
          {{ brand.name }}
        </a>
      </div>
    </div>
    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head">{{ p.name }}：</div>
      <div class="body">
        <a
          href="JavaScript:;"
          v-for="attr in p.properties"
          :key="attr.id"
          :class="{ active: p.selectedProp === attr.id }"
          @click="changeProp(p, attr.id)"
        >
          {{ attr.name }}
        </a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>

<style lang="scss" scoped>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: $xtxColor;
        }
      }
    }
  }
}

.xtx-skeleton {
  padding: 10px 0;
}
</style>
